import 'dart:convert';

import 'package:flutter_taotaoshipin/generated/json/base/json_convert_content.dart';
import 'package:flutter_taotaoshipin/value/constant.dart';

class AppProfile {
  int? chnId = int.parse(Constant.chnId);
  String? ver = Constant.ver;
  String? agentName = 'AppStore';
  bool? fuliBtnClicked = false;
  bool? shareBtnClicked = false;
  int? openAutoPlayTimestamp = 0;

  AppProfile();

  factory AppProfile.fromJson(Map<String, dynamic> json) {
    final AppProfile profile = AppProfile();
    final int? chnId = jsonConvert.convert<int>(json['chnId']);
    if (chnId != null) {
      profile.chnId = chnId;
    }
    final String? ver = jsonConvert.convert<String>(json['ver']);
    if (ver != null) {
      profile.ver = ver;
    }
    final String? agentName = jsonConvert.convert<String>(json['agentName']);
    if (agentName != null) {
      profile.agentName = agentName;
    }
    final bool? fuliBtnClicked =
        jsonConvert.convert<bool>(json['fuliBtnClicked']);
    if (fuliBtnClicked != null) {
      profile.fuliBtnClicked = fuliBtnClicked;
    }
    final bool? shareBtnClicked =
        jsonConvert.convert<bool>(json['shareBtnClicked']);
    if (shareBtnClicked != null) {
      profile.shareBtnClicked = shareBtnClicked;
    }
    final int? openAutoPlayTimestamp =
        jsonConvert.convert<int>(json['openAutoPlayTimestamp']);
    if (openAutoPlayTimestamp != null) {
      profile.openAutoPlayTimestamp = openAutoPlayTimestamp;
    }
    return profile;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['chnId'] = chnId;
    data['ver'] = ver;
    data['agentName'] = agentName;
    data['fuliBtnClicked'] = fuliBtnClicked;
    data['shareBtnClicked'] = shareBtnClicked;
    data['openAutoPlayTimestamp'] = openAutoPlayTimestamp;
    return data;
  }

  @override
  String toString() {
    return jsonEncode(this);
  }
}

extension AppProfileExtension on AppProfile {
  AppProfile copyWith({
    int? chnId,
    String? ver,
    String? agentName,
    bool? fuliBtnClicked,
    bool? shareBtnClicked,
    int? openAutoPlayTimestamp,
  }) {
    var profile = AppProfile();
    if (chnId != null && chnId != 0) {
      profile.chnId = chnId;
    }
    if (ver != null && ver.isNotEmpty) {
      profile.ver = ver;
    }
    if (agentName != null && agentName.isNotEmpty) {
      profile.ver = agentName;
    }
    profile.fuliBtnClicked = fuliBtnClicked ?? fuliBtnClicked;
    profile.shareBtnClicked = shareBtnClicked ?? shareBtnClicked;
    profile.openAutoPlayTimestamp = openAutoPlayTimestamp ?? 0;
    return profile;
  }
}
